#include <iostream>
#include <vector>

using namespace std;
/**
 * @brief
 * 1 <= s.length <= 105
 * s[i] is either '0' or '1'.
 *
 */
class Solution
{
public:
    /**
     * @brief 00110011
     *
     * @param s
     * @return int
     */
    int countBinarySubstrings(string s)
    {
        int sLen = s.length();
        int count = 1, ans = 0;
        for (int i = 0, j = 1; i < sLen && j < sLen; j++)
        {
            if (s[j] == s[i])
            {
                count++;
            }
            else
            {
                count--;
                if (0 == count)
                {
                    cout << s.substr(i, j - i + 1) << endl;
                    ans++;
                    i++;
                    j = i;
                    count = 1;
                }
                if (j + 1 == sLen && count < j - i)
                {
                    ans++;
                }
            }
        }
        return ans;
    }

    int countBinarySubstrings_2(string s)
    {
        vector<int> counts;
        int ptr = 0, n = s.size();
        while (ptr < n)
        {
            char c = s[ptr];
            int count = 0;
            while (ptr < n && s[ptr] == c)
            {
                ++ptr;
                ++count;
            }
            counts.push_back(count);
        }
      
    
        int ans = 0;
        for (int i = 1; i < counts.size(); ++i)
        {
            ans += min(counts[i], counts[i - 1]);
        }
        return ans;
    }
    int countBinarySubstrings_3(string s)
    {
        int ptr = 0, n = s.size(), last = 0, ans = 0;
        while (ptr < n)
        {
            char c = s[ptr];
            int count = 0;
            while (ptr < n && s[ptr] == c)
            {
                ++ptr;
                ++count;
            }
            ans += min(count, last);
            last = count;
        }
        return ans;
    }
};
int main()
{
    Solution s;
    cout << s.countBinarySubstrings_2("1111000") << endl;
    system("pause");
    return 0;
}
